﻿Public Class FrmDepTerm
    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")
    Dim nbchamps As Integer = 0
    Dim position As Integer = 0
    Dim idcli As Integer
    Dim Message, Titre As String

    Private Sub FrmDepTerm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.Top = 0
        Me.Left = 800
        connection()

        Rafraichir()
    End Sub

#Region "fonction"
    Public Function connection()
        Try
            macnx.Se_Connecter_MySqlServer()
            DataGridListeDep.DataSource = Nothing
            macnx.viderVue("interFini")
            DataGridListeDep.DataSource = macnx.Executer_Requete_Select("SELECT intervention.idIntervention AS Num , nomClient AS Nom, PrenomClient As Prenom, TypeIntervention AS TypeInter, TypeMateriel, Lieu, date.DateHeureReception AS Reception, date.dateHeureDebIntervention AS Debut_Intervention, Date.dateHeureFinIntervention AS Fin_Intervention, date.DateHeureLivraison AS Livraison, client.idclient , EtatIntervention FROM intervention, client, DATE WHERE ( intervention.idCli = client.idClient ) AND ( client.idClient = date.idcli ) AND ( intervention.idintervention = date.idIntervention ) AND ( etatIntervention =  'terminée' ) ORDER BY intervention.idIntervention asc", "interFini")
            macnx.Se_Deconnecter_MySqlServer()
            DataGridListeDep.AutoResizeColumns()
            CacherColonne(DataGridListeDep, "Fin_Intervention")
            CacherColonne(DataGridListeDep, "idclient")
            CacherColonne(DataGridListeDep, "EtatIntervention")
            CacherColonne(DataGridListeDep, "TypeMateriel")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Function

    Public Sub CacherColonne(data As DataGridView, colonne As String)

        data.Columns(colonne).Visible = False

    End Sub

    Public Function RemplaceSlashApostrophesDataTable(ByRef lesdonnees As DataTable)
        Dim nblignes As Integer
        Dim nbcolonnes As Integer
        Dim test As String = ""
        nblignes = lesdonnees.Rows.Count
        nbcolonnes = lesdonnees.Columns.Count
        Dim i, j As Integer
        For i = 0 To nblignes - 1
            For j = 0 To nbcolonnes - 1
                'Attention le replace ne s'applique qu'aux type String : utilisation de la classe de base Object :
                If Object.ReferenceEquals(lesdonnees.Rows(i).Item(j).GetType(), test.GetType()) Then
                    lesdonnees.Rows(i).Item(j) = RemplaceSlashApostrophes(lesdonnees.Rows(i).Item(j))
                End If
            Next
        Next
        Return lesdonnees
    End Function

    Public Function RemplaceApostrophesSlash(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("'", "/")
        Return ch
    End Function

    Public Function RemplaceSlashApostrophes(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("/", "'")
        Return ch
    End Function

    Public Sub Rafraichir()
        If (DataGridListeDep.RowCount - 1) <> 0 Then

            lblidClient.Text = DataGridListeDep.CurrentRow.Cells(10).Value.ToString()
            TextBoxNomClient.Text = DataGridListeDep.CurrentRow.Cells(1).Value.ToString()
            TextBoxPrenomClient.Text = DataGridListeDep.CurrentRow.Cells(2).Value.ToString()
            TextBoxTypInt.Text = DataGridListeDep.CurrentRow.Cells(3).Value.ToString()
            TextBoxTypPc.Text = DataGridListeDep.CurrentRow.Cells(4).Value.ToString()
            TextBoxLieu2.Text = DataGridListeDep.CurrentRow.Cells(5).Value.ToString()
            lblIdIntervention.Text = DataGridListeDep.CurrentRow.Cells(0).Value.ToString()

            macnx.Se_Connecter_MySqlServer()
            If (DataGridListeDep.CurrentRow.Cells(5).Value.ToString() = "domicile") Or (DataGridListeDep.CurrentRow.Cells(3).Value.ToString() = "formation") Then
                TextBoxTemps.Text = DataGridListeDep.CurrentRow.Cells(8).Value.ToString()
                Label6.Text = "Date RDV :"
            Else
                TextBoxTemps.Text = macnx.Executer_Requete_TIMEDIFF_Finis(lblIdIntervention.Text, "DateHeureFinIntervention", "DateHeureReception")
                TextBoxRecap.Text = macnx.ExecuteScalar("SELECT EnonceProbleme FROM intervention where idcli = " & lblidClient.Text & "")
                Label6.Text = "Temps intervention :"
            End If
            macnx.Se_Deconnecter_MySqlServer()
        End If
    End Sub


#End Region

    Private Sub BtnValider_Click(sender As System.Object, e As System.EventArgs) Handles BtnValider.Click
        Message = "ètes vous sur de vouloir facturer l'intervention " & lblIdIntervention.Text & " du client " & TextBoxNomClient.Text & " " & TextBoxPrenomClient.Text
        If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then

            macnx.Se_Connecter_MySqlServer()
            macnx.Executer_Requete_Action("update intervention set etatIntervention = 'facturée'  where idintervention = " & lblIdIntervention.Text & "")
            macnx.Se_Deconnecter_MySqlServer()
            MessageBox.Show("L'intervention sélectionnée est facturée", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            connection()
        End If
    End Sub

    Private Sub BtnTicket_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub DataGridListeDep_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridListeDep.CellClick
        Rafraichir()
    End Sub

    Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
        connection()

        Rafraichir()
    End Sub
End Class